package com.gm.wj.util;

import com.gm.wj.FrontEndTemplate.MessageTemplate;
import com.gm.wj.dao.SysStudentDAO;
import com.gm.wj.entity.User;
import com.gm.wj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @program: wj
 * @description: 邮件工具
 * @author: Huang_shengjun
 * @create: 2020-11-10 17:32
 **/
@Slf4j
public class SendByEmailTools {


    public static String senderEmailAccount = "1@qq.com"; 		// 发件人邮箱账号
    public static String senderEmailPassword = ""; 	// 发件人邮箱授权码
    public static String senderEmailSMTPHost = "smtp.qq.com"; 	 		// 邮箱服务器

    public static void main(String[] args) throws Exception {
        // sendMail("shengjun15@163.com","主题","内容111111111111");
        sendMailAndAppendix("s@163.com","主题","内容111111111111","C:\\Users\\15329\\Desktop\\每日任务！.txt");
    }

    /**
     *
     * DESC : 发送邮件(邮件正文)
     * 	receiveMailAccount 	接收者邮箱账号
     * 	subject				邮件主题
     * 	emilContent			邮件正文内容(支持HTML)
     *  receiveMailAccount  接收邮件帐户  subject 主题  emilContent  emil内容
     */
    public static void sendMail(String receiveMailAccount, String subject, String emilContent) throws Exception {

        log.info("发送邮件(邮件正文) -- 执行");

        subject = "【教育培训机构】 " + subject;
        emilContent = "【教育培训机构】 " + emilContent;

        MimeMultipart mimeMultipart = new MimeMultipart();

        /*添加邮件正文*/
        MimeBodyPart contentPart = new MimeBodyPart();
        /*UTF-8 防止乱码*/
        contentPart.setText(emilContent,"UTF-8");
        contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");
        mimeMultipart.addBodyPart(contentPart);

        String sender = "";
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");							//设置传输协议
        props.setProperty("mail.smtp.host", senderEmailSMTPHost); 						//设置发件人的SMTP服务器地址
        props.setProperty("mail.smtp.auth", "true");									//设置用户的认证方式
        Session session = Session.getDefaultInstance(props);
        session.setDebug(false);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(senderEmailAccount, sender,"UTF-8"));		// 邮箱账号
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(
                receiveMailAccount, "", "UTF-8"));
        message.setSubject(subject, "UTF-8");
        message.setContent(mimeMultipart);
        message.setSentDate(new Date());
        message.saveChanges();
        Transport transport = session.getTransport();
        transport.connect(senderEmailAccount, senderEmailPassword); 					// 登录邮箱
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }



    /**
     *
     * DESC : 发送邮件(正文+包含附件)，如果附件不存在或路径错误，则邮件中不会显示附件
     *     receiveMailAccount    接收者邮箱账号
     * 	   subject		                  邮件主题
     * 	   emilContent			  邮件正文内容(支持HTML)
     * 	   appendixURL	                  附件路径
     *     receiveMailAccount  接收邮件帐户  subject 主题  emilContent  emil内容  appendixURL 附录地址
     * @return
     */
    public static Map<String, Object> sendMailAndAppendix(String receiveMailAccount, String subject, String emilContent, String appendixURL) throws Exception {

        log.info("发送邮件(正文+包含附件) -- 执行");
        subject = "【教育培训机构】 " + subject;
        emilContent = "【教育培训机构】 " + emilContent;

        MimeMultipart mimeMultipart = new MimeMultipart();

        /* 添加邮件正文 */
        MimeBodyPart contentPart = new MimeBodyPart();
        /*UTF-8 防止乱码*/
        contentPart.setText(emilContent,"UTF-8");
        contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");
        mimeMultipart.addBodyPart(contentPart);

        /* 添加附件 */
        File file = new File(appendixURL);
        String fileName = MimeUtility.encodeWord(file.getName());	//文件存在则获取文件名并进行乱码处理
        if(file.exists()){
            MimeBodyPart appendix = new MimeBodyPart();
            appendix.setDataHandler(new DataHandler(new FileDataSource(appendixURL)));
            appendix.setHeader("Content-Type", "text/html; charset=UTF-8");
            appendix.setFileName(fileName);								//设置附件文件名(和原文件名一致)
            mimeMultipart.addBodyPart(appendix);
        }else{
            fileName = "附件不存在或路径错误！";
            log.info("附件不存在或路径错误！");
        }


        String sender = "";
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");							//设置传输协议
        props.setProperty("mail.smtp.host", senderEmailSMTPHost); 						//设置发件人的SMTP服务器地址
        props.setProperty("mail.smtp.auth", "true");									//设置用户的认证方式
        Session session = Session.getDefaultInstance(props);
        session.setDebug(false);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(senderEmailAccount, sender,"UTF-8"));		// 邮箱账号
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMailAccount, "", "UTF-8"));
        message.setSubject(subject, "UTF-8");
        message.setContent(mimeMultipart);
        message.setSentDate(new Date());
        message.saveChanges();
        Transport transport = session.getTransport();
        transport.connect(senderEmailAccount, senderEmailPassword); 					// 登录邮箱
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("fileName",fileName);
        return map;
    }

    public static void sendMailToOrder(String user,String orderId,String email,String time,String courserName,String teacher,String amount, String subject) throws Exception {

        log.info("正在发送订单邮件！");
        String emilContent = MessageTemplate.MessageOrder( user, orderId, email, time, courserName, teacher, amount);

        MimeMultipart mimeMultipart = new MimeMultipart();

        /*添加邮件正文*/
        MimeBodyPart contentPart = new MimeBodyPart();
        /*UTF-8 防止乱码*/
        contentPart.setText(emilContent,"UTF-8");
        contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");
        mimeMultipart.addBodyPart(contentPart);

        String sender = "";
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");							//设置传输协议
        props.setProperty("mail.smtp.host", senderEmailSMTPHost); 						//设置发件人的SMTP服务器地址
        props.setProperty("mail.smtp.auth", "true");									//设置用户的认证方式
        Session session = Session.getDefaultInstance(props);
        session.setDebug(false);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(senderEmailAccount, sender,"UTF-8"));		// 邮箱账号
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(
                email, "", "UTF-8"));
        message.setSubject(subject, "UTF-8");
        message.setContent(mimeMultipart);
        message.setSentDate(new Date());
        message.saveChanges();
        Transport transport = session.getTransport();
        transport.connect(senderEmailAccount, senderEmailPassword); 					// 登录邮箱
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

}
